package com.foreveross.service;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.imageio.stream.FileImageInputStream;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.collections.iterators.ArrayListIterator;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.foreveross.SYWebService.SYHttpclientCall;
import com.foreveross.SYWebService.vo.ConditionVo;
import com.foreveross.SYWebService.vo.DataVo;
import com.foreveross.SYWebService.vo.P7;
import com.foreveross.SYWebService.vo.Result;
import com.foreveross.SYWebService.vo.SYResult;
import com.foreveross.SYWebService.vo.SYSWin;
import com.foreveross.SYWebService.vo.SYSWinWithP7;
import com.foreveross.common.CommonUtil;
import com.foreveross.common.Constants;
import com.foreveross.dao.HouseRepairDAO;
import com.foreveross.dao.HouseUserDAO;
import com.foreveross.dao.HrHouseInfoDAO;
import com.foreveross.util.Base64ImageUtil;
import com.foreveross.util.JSONUtil;
import com.foreveross.util.ObjectToMap;
import com.foreveross.vo.HouseRepair;
import com.foreveross.vo.HouseUser;
import com.foreveross.vo.HrHouses;
import com.foreveross.vo.common.PageVo;
import com.foreveross.vo.common.ResultVo;

@Service("houseRepairService")
@Transactional
public class HouseRepairService extends BaseService {

	@Resource
	public HouseRepairDAO houseRepairDAO;
	@Resource
	public HrHouseInfoDAO hrHouseInfoDAO;
	@Resource
	public HouseUserDAO houseUserDAO;

	public List<HouseRepair> findHouseRepairList(String repair_no, String name,
			String phone, String type,String typeName, String status, String houseId,
			String resCode,int pageIndex, int pageSize, String userId) throws Exception {
		return houseRepairDAO.findHouseRepairList(repair_no, name, phone, type, typeName,
				status, houseId,resCode, pageIndex, pageSize, userId);
	}

	public void getMyHouseRepair(String weixinid, String currentPage,
			String pageSize, ResultVo resultVo) throws Exception {
		PageVo<HouseRepair> page = houseRepairDAO.getMyHouseRepair(weixinid,
				currentPage, pageSize);
		if (page.getData() != null && !page.getData().isEmpty()) {
			resultVo.setData(page);
		} else {
			resultVo.setData(new ArrayList());
			resultVo.setMessage("获取物业报修列表为空！");
		}
		resultVo.setSuccess(true);
	}

	// 获取报修类型列表
	public Map<String, Object> synUserRev_GetServiceMainType(ConditionVo conditionVo)
			throws Exception {
		List<DataVo> dataVoList = new ArrayList<DataVo>();
		Map<String, String> params = new HashMap<String, String>();
		params = ObjectToMap.objectToMapViaFields(conditionVo, true);
		SYResult sysResult = SYHttpclientCall.getSYData(params);
		List<SYSWin> list = sysResult.getUserRev_GetServiceMainType();
		if (list != null && !list.isEmpty()) {
			SYSWin sYSWin = list.get(0);
			dataVoList = sYSWin.getSyswin();
		}
		
		List<Map<String, Object>> listv = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		for (DataVo dataVo : dataVoList) {
			Map<String, Object>  mapO  = new HashMap<String, Object>();
			mapO.put("chooseItem", dataVo.getMainTypeName());
			mapO.put("chooseValue", dataVo.getMainTypeID());
			listv.add(mapO);
		}

		map.put("data", listv);
		map.put("total", listv.size());
		
		return map;
	}

	// 查看详细报修进度
	public List<DataVo> synUser_GetServiceDetail(ConditionVo conditionVo)
			throws Exception {
		List<DataVo> dataVoList = new ArrayList<DataVo>();
		Map<String, String> params = new HashMap<String, String>();
		params = ObjectToMap.objectToMapViaFields(conditionVo, true);
		SYResult sysResult = SYHttpclientCall.getSYData(params);
		List<SYSWin> list = sysResult.getUser_GetServiceDetail();
		if (list != null && !list.isEmpty()) {
			SYSWin sYSWin = list.get(0);
			dataVoList = sYSWin.getSyswin();
		}
		return dataVoList;
	}

	// 报修
	public DataVo synHouseRepair(HouseRepair houseRepair, String houseId,
			String houseUserId, HttpServletRequest request) throws Exception {
		HrHouses hrHouses = (HrHouses) hrHouseInfoDAO.findById(HrHouses.class,
				houseId);
		HouseUser houseUser = (HouseUser) houseUserDAO.findById(
				HouseUser.class, houseUserId);
		if (houseRepair != null) {
			ConditionVo conditionVo = new ConditionVo();
			conditionVo.setP0("User_ServiceSave");
			P7 p7 = new P7();
			p7.setOrgID(hrHouses.getProjectId());
			p7.setOrgName(hrHouses.getName());
			p7.setCttID(houseUser.getCttId());
			p7.setWOID(houseUser.getResCode());
			p7.setWONoBasicID("");
			p7.setElements(houseRepair.getContent());
			p7.setCallPhone(houseRepair.getPhone());

			String url = houseRepair.getUrl();
			if (url != null && !url.equals("")) {
				String urlArray[] = url.split(",");
				int maxNum = urlArray.length > 2 ? 2 : urlArray.length;
				
				for (int i = 0; i < maxNum; i++) {
					String filePath = CommonUtil.getRealPath(request)
							+ urlArray[i];
					System.out.println("图片思源上传获取地址："+filePath);
					String Base64Str = Base64ImageUtil.GetImageStr(filePath);
					if(i==0) p7.setPicture1(Base64Str);
					if(i==1) p7.setPicture2(Base64Str);
				}
				if(maxNum == 1) p7.setPicture2("");
			}else{
				p7.setPicture1("");
				p7.setPicture2("");
			}
			
			SYSWinWithP7 sYSWinWithP7 = new SYSWinWithP7();
			List<P7> p7List = new ArrayList<P7>();
			p7List.add(p7);
			sYSWinWithP7.setSyswin(p7List);
			conditionVo.setP7(JSONUtil.toJsonWithGsonBuilderBySerializeNulls(sYSWinWithP7));
			
			Map<String, String> params = ObjectToMap.objectToMapViaFields(
					conditionVo, true);
			SYResult sysResult = SYHttpclientCall.getSYData(params);
			List<Result> list = sysResult.getUser_ServiceSave();
			if (list != null && !list.isEmpty()) {
				Result result = list.get(0);
				List<DataVo> listv = result.getResult();
				if (listv != null && !listv.isEmpty()) {
					DataVo datavo = listv.get(0);
					return datavo;
				}
			}
		}
		return null;
	}

	public long getHouseRepairListCount(String repair_no, String name,
			String phone, String type,String typeName, String status, String houseId,
			String resCode,String userId) throws Exception {
		return houseRepairDAO.getHouseRepairCount(repair_no, name, phone, type, typeName,
				status, houseId, resCode,userId);
	}
}
